package com.qf.sportsevent.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;

import com.qf.sportsevent.R;
import com.qf.sportsevent.adapter.SubscribeRecyclerAdapter;
import com.qf.sportsevent.databinding.ActivitySubscribeBinding;
import com.qf.sportsevent.util.FastEventBus;
import com.qf.sportsevent.util.RecyclerItemLayoutManager;
import com.qf.sportsevent.widget.OnRecyclerItemClickListener;
import com.qf.sportsevent.widget.RecyclerItemTouchHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class SubscribeActivity extends AppCompatActivity {

    private ArrayList<String> channelNames;
    private ActivitySubscribeBinding binding;
    private SubscribeRecyclerAdapter<Object> adapter;
    private ItemTouchHelper itemTouchHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_subscribe);
        channelNames = getIntent().getStringArrayListExtra("channelNames");
        ArrayList<Object> list = new ArrayList<>();
        adapter = new SubscribeRecyclerAdapter<>(this, list, RecyclerItemLayoutManager.getLayoutMap());
        binding.recycler.setAdapter(adapter);
        itemTouchHelper = new ItemTouchHelper(new RecyclerItemTouchHelper<>(list,false));
        itemTouchHelper.attachToRecyclerView(binding.recycler);
        binding.recycler.addOnItemTouchListener(new OnRecyclerItemClickListener(binding.recycler) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder,View item, float clickX) {
                int[] location = new int[2];
                item.getLocationInWindow(location);
                View removeView = item.findViewById(R.id.subscribe_remove);
                float marginLeft = getResources().getDimension(R.dimen.subscribe_remove_margin);
                if(removeView != null && clickX > location[0] + marginLeft && clickX < location[0] + marginLeft +removeView.getWidth()){
                    adapter.remove(viewHolder.getLayoutPosition());
                }
            }
            @Override
            public void onItemLongClick(RecyclerView.ViewHolder viewHolder,View item, float clickX) {
                int[] location = new int[2];
                item.getLocationInWindow(location);
                View moveView = item.findViewById(R.id.subscribe_move);
                if(moveView != null && clickX < location[0]+item.getWidth() && clickX > location[0]+item.getWidth()-moveView.getWidth()){
                    itemTouchHelper.startDrag(viewHolder);
                }
            }
        });

        ArrayList<String> headItem = new ArrayList<>();
        headItem.add("我关注的栏目");
        headItem.add("按住右侧按钮可拖拽排序");
        adapter.add(headItem);
        if (channelNames != null) {
            adapter.addAll(channelNames);
        }
        ArrayList<String> footItem = new ArrayList<>();
        footItem.add("未关注的栏目");
        footItem.add("取消关注后将不会收到该栏目的推送消息");
        adapter.add(footItem);
    }

    @Override
    public void onBackPressed() {
        List<Object> list = adapter.getData();
        List<String> changedList = new ArrayList<>();
        for (int i = 1; i < list.size()-1; i++) {
            changedList.add(((String) list.get(i)));
        }
        List<Integer> indexList=new ArrayList<>();
        for (int i = 0; i < changedList.size(); i++) {
            String s = changedList.get(i);
            for (int j = 0; j < channelNames.size(); j++) {
                if(s.equals(channelNames.get(j))){
                    indexList.add(j);
                    break;
                }
            }
        }
        FastEventBus subScribeBus = new FastEventBus();
        subScribeBus.setSubscribeIndexs(indexList);
        subScribeBus.setTag("subscribe");
        EventBus.getDefault().post(subScribeBus);
        super.onBackPressed();
    }
}
